<template lang="pug">
.pacs-critical-value
  Dialog(
    v-model="model"
    cancel-text="取消"
    size="mini"
    title="危急值说明"
    v-bind:onCancel="onCancel"
    v-bind:onConfirm="onConfirm"
    v-bind:onClose="onClose",
    :hide-footer="false"
    :fullscreen="false"
    confirm-text="保存"
    :confirm-loading="loadingMap['save']"
  )
    mt-input(type="textarea",:rows="3",placeholder="请输入危急值",v-model="editingItem.diagnosticOpinion")
</template>

<script setup lang="ts">
import { Dialog, useNotify } from "@scmt/common-components";
import { useApi } from "@scmt/core-api";
import { useRequest } from "@scmt/common-hooks";
import { useUser } from "@scmt/common-store";
import type { RegistrationEntity } from "@scmt/api-pacs-registration";
import { useUtils } from "@scmt/lib-utils";
import { ESourceStatus } from "@scmt/api-common";

const $utils = useUtils();
const user = useUser();
const $api = useApi();
const editingItem = ref<RegistrationEntity>($api.pacs.registration.$$createEntity({}));
const $notify = useNotify();
const { request, loadingMap } = useRequest({
  //保存危急值
  save: (entity: any) => {
    return $api.lis.criticalValue.batchCriticalValue(entity);
  },

});


//添加 Props属性
const props = defineProps({
  selectItem: Object,
});
const model = defineModel({
  default: false,
});

//弹框点击确认事件
const onConfirm = () => {
  if (!editingItem.value.diagnosticOpinion || editingItem.value.diagnosticOpinion.trim().length == 0) {
    $notify.error("请先输入危急值！！");
    return;
  }
  let entity = {
    id: $utils.uuid(),
    patientName: editingItem.value.name,
    specimenId: editingItem.value.id,
    patientId: editingItem.value.patientId,
    reportId: editingItem.value.applyDoctorId,
    outpatientNumber: editingItem.value.outpatientNumber,
    baseProjectId: editingItem.value.id,
    sourceId: editingItem.value.id,
    baseProjectList: [],
    source: ESourceStatus.PACS,
  };
  //@ts-ignore
  entity.baseProjectList.push({
    specimenId: editingItem.value.id,
    patientId: editingItem.value.patientId,
    baseProjectName: editingItem.value.projectName,
    unit: "-",
    baseProjectId: editingItem.value.id,
    reference: "未见明显异常",
    result: editingItem.value.diagnosticOpinion,
    state: "异常",
    criticalValueId: entity.id,
    reportId: user.info.userId,
  });
  request("save", entity).then((res) => {
    if (res) {
      $notify.success("保存成功");
      model.value = false;
    }
  })
  //model.value = false;
};
//关闭弹框
const onCancel = () => {
  model.value = false;
};
//取消
const onClose = () => {
  model.value = false;
};

watch(
  model,
  () => {
    if (model.value) {
      editingItem.value = $api.pacs.registration.$$createEntity(props.selectItem)
    }
  },
  {
    immediate: true,
  }
);
</script>

<style lang="less" scoped></style>